import 'package:flutter/material.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/controllers/my_controller/gratitude_controller/cratitude_record_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:xinxuan/vo/gratitudes_vo.dart';

class GratitudeRecordPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: '回馈感恩值明细',
        backgroundColor: pageBgColor,
      ),
      body: GetBuilder<GratitudeRecordPageController>(
        init: GratitudeRecordPageController(),
        builder: (controller) {
          return Column(
            children: [
              Container(
                height: 47.h,
                padding: EdgeInsets.symmetric(horizontal: 80.w),
                decoration: BoxDecoration(
                  color: bgWhiteColor,
                  border: Border(
                    bottom: BorderSide(
                      color: lineColor,
                      width: .5
                    )
                  )
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text.rich(
                      TextSpan(
                        text: '余额：',
                        style: TextStyle(
                          color: textSubtitleColor,
                          fontSize: 12.sp
                        ),
                        children: [
                          TextSpan(
                            text: '${controller.gratitudesVo.value.meta!.aggregates!.balance}',
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 16.sp
                            )
                          )
                        ]
                      )
                    ),
                    Text.rich(
                      TextSpan(
                        text: '已到账：',
                        style: TextStyle(
                          color: textSubtitleColor,
                          fontSize: 12.sp
                        ),
                        children: [
                          TextSpan(
                            text: '${controller.gratitudesVo.value.meta!.aggregates!.received}',
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 16.sp
                            )
                          )
                        ]
                      )
                    )
                  ],
                ),
              ),
              Expanded(child: RefreshUtil.easyRefresh(
                onRefresh: () async {
                  controller.getGratitudeRecord();
                },
                onLoad: () async {
                  controller.onloadGratitudeRecord();
                },
                slivers: controller.gratitudesVo.value.data!.map((item) {
                  return SliverToBoxAdapter(
                    child: Column(
                      children: item.map((dataItem) {
                        return dataItem.type == 1 ? _incomeItem(dataItem) : _feedbackItem(dataItem);
                      }).toList(),
                    ),
                  );
                }).toList()
              ))
            ],
          );
        }
      )
    );
  }

  Widget _incomeItem(GratitudesItem dataItem) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: bgWhiteColor,
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 14.h, horizontal: 4.w),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: .5,
              color: lineColor
            )
          )
        ),
        child: Row(
          children: [
            Image.asset(
              'assets/icons/icon_income.png',
              width: 36.w,
              height: 36.w,
            ),
            SizedBox(width: 12.w),
            Expanded(child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '${dataItem.title}',
                  style: TextStyle(
                    color: textBlackColor,
                    fontSize: 16.sp,
                    fontWeight: FontWeight.bold
                  ),
                ),
                SizedBox(height: 4.h),
                Text(
                  '${dataItem.createdAt}',
                  style: TextStyle(
                    color: textSubtitleColor,
                    fontSize: 12.sp
                  ),
                )
              ],
            )),
            Text(
              '+${dataItem.totalAmount}',
              style: TextStyle(
                color: textSuccessColor,
                fontSize: 16.sp
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _feedbackItem(GratitudesItem dataItem) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: bgWhiteColor,
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 14.h, horizontal: 4.w),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: .5,
              color: lineColor
            )
          )
        ),
        child: Row(
          children: [
            Image.asset(
              'assets/icons/icon_gratitude.png',
              width: 36.w,
              height: 36.w,
            ),
            SizedBox(width: 12.w),
            Expanded(child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text.rich(
                  TextSpan(
                    text: '${dataItem.title}',
                    style: TextStyle(
                      color: textBlackColor,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold
                    ),
                    children: [
                      WidgetSpan(child: Container(width: 6.w)),
                      TextSpan(
                        text: '${dataItem.createdAt}',
                        style: TextStyle(
                          color: textSubtitleColor,
                          fontSize: 12.sp,
                          fontWeight: FontWeight.normal
                        )
                      )
                    ]
                  ),
                ),
                SizedBox(height: 4.h),
                Text(
                  '关联订单: ${dataItem.orderSn}',
                  style: TextStyle(
                    color: textBlackColor,
                    fontSize: 12.sp
                  )
                ),
                SizedBox(height: 4.h),
                Text(
                   '进度: ${dataItem.progress}',
                  style: TextStyle(
                    color: mainTextColor,
                    fontSize: 12.sp
                  )
                ),
              ],
            )),
            Text(
              '${dataItem.totalAmount}',
              style: TextStyle(
                color: mainTextColor,
                fontSize: 16.sp
              ),
            )
          ],
        ),
      ),
    );
  }
}